# -*- coding: utf-8 -*-
# @Time    : 2019/11/15 5:11 下午
# @Author  : CHENlj
# @File    : Func.py

from Common import Consts
from Common.Request import Request
import requests


def get_user_addr(currency_id=None):
    """
    获取用户的提币地址信息
    :param currency_id:
    :return:
    """
    headers = {
        'Authorization': Consts.TOKEN_FOREGROUND
    }

    if currency_id is None:
        return Request.get_request(Consts.ADDRESS_BOOK, headers=headers)
    else:
        params = {
            'currency_id': int(currency_id)
        }
        return Request.get_request(Consts.ADDRESS_BOOK, params=params, headers=headers)


def get_payments():
    """
    获取用户的收付款地址
    :return:
    """
    headers = {
        'Authorization': Consts.TOKEN_FOREGROUND
    }
    return Request.get_request(Consts.GET_PAYMENTS, headers=headers)


def get_lottery_info():
    """
    获取抽奖页面详情
    :return:
    """
    headers = {
        'Authorization': Consts.TOKEN_FOREGROUND
    }
    return Request.get_request(Consts.LOTTERY_INFO, headers=headers)


def get_integral_info():
    """
    获取用户个人积分
    :return:
    """
    headers = {
        'Authorization': Consts.TOKEN_FOREGROUND
    }
    return Request.get_request(Consts.INTEGRAL_INFO, headers=headers)


def get_commodity_list_foreground(num, page, per_page):
    """
    前台获取商品列表
    :param num:
    :param page:
    :param per_page:
    :return:
    """
    headers = {
        'Authorization': Consts.TOKEN_FOREGROUND
    }
    params = {
        'page': page,
        'per_page': per_page
    }
    return Request.get_request(Consts.COMMODITY_LIST_FORE, params=params, headers=headers)


def get_commodity_info_foreground(id):
    """
    前台获取商品详情
    :param id:
    :return:
    """
    headers = {
        'Authorization': Consts.TOKEN_FOREGROUND
    }
    return Request.get_request(Consts.COMMODITY_INFO_FORE+str(id), headers=headers)


def get_commodity_list_background(type=None, page=None, per_page=None):
    """
    后台获取商品列表
    :param per_page:
    :param page:
    :param type:
    :return:
    """
    headers = {
        'Authorization': Consts.TOKEN_BACKGROUND
    }
    if type is None:
        return Request.get_request(Consts.COMMODITY_LIST_BACK, headers=headers)
    else:
        params = {
            'type': type,
            'page': page,
            'per_page': per_page
        }
        return Request.get_request(Consts.COMMODITY_LIST_BACK, params, headers)


if __name__ == '__main__':
    res = get_commodity_list_background()
    # print(str(type(res['list'])) == "<class 'list'>")
    # print(res[-1:-2:-1][0]['id'])
    # exist_ids = []
    # for item in res:
    #     exist_ids.append(item['id'])
    # print(exist_ids)

    cookie = requests.utils.dict_from_cookiejar(res.cookies)
    print(cookie)
